/** 所有用户完成的结果 */
const AllUserResults = {}

/** 所有分数 */
const AllScore = {}

/** 等级对应分数 */
const LEVEL_SCORE = {
  1: 2,
  2: 3,
  3: 6,
  4: 12,
  5: 24
}

/**
 * 完成题目
 */
function finishQuestion(data) {
  const { username, level, id } = data

  if (!Reflect.has(AllUserResults, username)) {
    AllUserResults[username] = []
    AllScore[username] = 0
  }
  // 根据id判断是否已经有相同的题目了
  if (AllUserResults[username].findIndex((item) => item.id === id) > -1)
    return false

  AllUserResults[username].push({
    id,
    level
  })

  // 添加完题目之后，然后计算分数
  AllScore[username] += LEVEL_SCORE[level]

  return true
}

/**
 * 获取所有分数
 */
function getAllScore() {
  return AllScore
}

/**
 * 清除用户的信息
 */
function clearUser(username) {
  if (Reflect.has(AllScore, username)) {
    Reflect.deleteProperty(AllScore, username)
  }
  if (Reflect.has(AllUserResults, username)) {
    Reflect.deleteProperty(AllUserResults, username)
  }
}

module.exports = {
  finishQuestion,
  getAllScore,
  clearUser
}
